package com.ruoyi.bussiness.service.impl;

import java.util.List;

import com.ruoyi.bussiness.domain.SysBusinessDynamicType;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bussiness.mapper.SysBusinessDynamicTypeMapper;
import com.ruoyi.bussiness.domain.SysBusinessDynamicType;
import com.ruoyi.bussiness.service.ISysBusinessDynamicTypeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务-动态类别Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-12
 */
@Service
public class SysBusinessDynamicTypeServiceImpl implements ISysBusinessDynamicTypeService
{
    @Autowired
    private SysBusinessDynamicTypeMapper sysBusinessDynamicTypeMapper;

    /**
     * 查询业务-动态类别
     *
     * @param dynamicTypeId 业务-动态类别主键
     * @return 业务-动态类别
     */
    @Override
    public SysBusinessDynamicType selectSysBusinessDynamicTypeByDynamicTypeId(Long dynamicTypeId)
    {
        return sysBusinessDynamicTypeMapper.selectSysBusinessDynamicTypeByDynamicTypeId(dynamicTypeId);
    }

    /**
     * 查询业务-动态类别列表
     *
     * @param sysBusinessDynamicType 业务-动态类别
     * @return 业务-动态类别
     */
    @Override
    public List<SysBusinessDynamicType> selectSysBusinessDynamicTypeList(SysBusinessDynamicType sysBusinessDynamicType)
    {
        return sysBusinessDynamicTypeMapper.selectSysBusinessDynamicTypeList(sysBusinessDynamicType);
    }

    /**
     * 新增业务-动态类别
     *
     * @param sysBusinessDynamicType 业务-动态类别
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysBusinessDynamicType(SysBusinessDynamicType sysBusinessDynamicType)
    {
        sysBusinessDynamicType.setCreateTime(DateUtils.getNowDate());
        sysBusinessDynamicType.setCreateBy(SecurityUtils.getUsername());
        sysBusinessDynamicType.setIsBottom("0");
        sysBusinessDynamicType.setIsTop("0");

        sysBusinessDynamicTypeMapper.insertSysBusinessDynamicType(sysBusinessDynamicType);

        sysBusinessDynamicTypeMapper.updateOrderNum(sysBusinessDynamicType.getDynamicTypeId());
        return 1;
    }

    /**
     * 修改业务-动态类别
     *
     * @param sysBusinessDynamicType 业务-动态类别
     * @return 结果
     */
    @Override
    public int updateSysBusinessDynamicType(SysBusinessDynamicType sysBusinessDynamicType)
    {
        sysBusinessDynamicType.setUpdateTime(DateUtils.getNowDate());
        sysBusinessDynamicType.setUpdateBy(SecurityUtils.getUsername());
        return sysBusinessDynamicTypeMapper.updateSysBusinessDynamicType(sysBusinessDynamicType);
    }

    /**
     * 批量删除业务-动态类别
     *
     * @param dynamicTypeIds 需要删除的业务-动态类别主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessDynamicTypeByDynamicTypeIds(Long[] dynamicTypeIds)
    {
        return sysBusinessDynamicTypeMapper.deleteSysBusinessDynamicTypeByDynamicTypeIds(dynamicTypeIds);
    }

    /**
     * 删除业务-动态类别信息
     *
     * @param dynamicTypeId 业务-动态类别主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessDynamicTypeByDynamicTypeId(Long dynamicTypeId)
    {
        return sysBusinessDynamicTypeMapper.deleteSysBusinessDynamicTypeByDynamicTypeId(dynamicTypeId);
    }

    @Override
    public AjaxResult moveUpOrDown(SysBusinessDynamicType entity) {
        SysBusinessDynamicType moveEntity = sysBusinessDynamicTypeMapper.selectSysBusinessDynamicTypeByDynamicTypeId(entity.getDynamicTypeId());
        if(moveEntity == null){
            return AjaxResult.error("动态类别数据不存在，不能上移下移");
        }
        SysBusinessDynamicType moveChangeEntity = null;
        if("up".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessDynamicTypeMapper.getLastList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("首行数据不能上移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessDynamicTypeMapper.updateSysBusinessDynamicType(moveEntity);
            sysBusinessDynamicTypeMapper.updateSysBusinessDynamicType(moveChangeEntity);
        }else if("down".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessDynamicTypeMapper.getNextList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("末行数据不能下移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessDynamicTypeMapper.updateSysBusinessDynamicType(moveEntity);
            sysBusinessDynamicTypeMapper.updateSysBusinessDynamicType(moveChangeEntity);
        }else{
            return AjaxResult.error("无效的移动方式");
        }
        return AjaxResult.success("移动成功",1);
    }

    @Override
    public AjaxResult pinToTopOrEnd(SysBusinessDynamicType entity) {
        SysBusinessDynamicType moveEntity = sysBusinessDynamicTypeMapper.selectSysBusinessDynamicTypeByDynamicTypeId(entity.getDynamicTypeId());
        if(moveEntity == null){
            return AjaxResult.error("动态类别不存在，不能上移下移");
        }
        if("top".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsTop())){
                moveEntity.setIsTop("0");
                moveEntity.setIsBottom("0");
            }else{
                moveEntity.setIsTop("1");
                moveEntity.setIsBottom("0");
            }
            sysBusinessDynamicTypeMapper.updateSysBusinessDynamicType(moveEntity);
        }else if("end".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsBottom())){
                moveEntity.setIsBottom("0");
                moveEntity.setIsTop("0");
            }else{
                moveEntity.setIsBottom("1");
                moveEntity.setIsTop("0");
            }
            sysBusinessDynamicTypeMapper.updateSysBusinessDynamicType(moveEntity);
        }else{
            return AjaxResult.error("无效的操作方式");
        }
        return AjaxResult.success("置顶置底成功",1);
    }
}
